<?php
/**
 * Created by PhpStorm.
 * User: Vova
 * Date: 9/28/14
 * Time: 9:59 PM
 */

//namespace controllers;

include_once("Controller.class.php");
require_once("C:/xampp/htdocs/door-market/php/models/Catalog_model.class.php");
require_once("C:/xampp/htdocs/door-market/php/views/Catalog_view.php");

require_once("C:/xampp/htdocs/door-market/php/classes/Host_config.class.php");

class Catalog_controller extends Controller{

    private $page = 15;
    private $sort = "cheap";
    private $pagesize = 15;

    function __construct()
    {
        $this->model = new Catalog_model();
        $this->view = new Catalog_view();
    }
    public function doors($sub_category,$args){
        $category = 'doors';
        $this->action($category,$sub_category,$args);
    }
    public function beds($sub_category,$args){
        $category = 'beds';
        $this->action($category,$sub_category,$args);

    }
    public function stairs($sub_category,$args){
        $category = 'stairs';
        $this->action($category,$sub_category,$args);

    }
    public function fires($sub_category,$args){
        $category = 'fires';
        $this->action($category,$sub_category,$args);
    }
    public function tables($sub_category,$args){
        $category = 'tables';
        $this->action($category,$sub_category,$args);
    }

    public  function action($category,$sub_category,$args){
        $this->parse_args($args);

        $catalog_items = $this->model->get_catalog_items($category,$sub_category,$this->page,$this->pagesize);

        $last_page_number = $this->model->get_catalog_last_page_number($category,$sub_category,$this->page,$this->pagesize);
        $first_page_mumber = 1;

        $previous_page_number = (($this->page - 1)>0)?($this->page - 1):1;
        $next_page_number  = (($this->page +1 )<$last_page_number)?($this->page + 1):$last_page_number;




        $pages = array(
            array("title" => 'first page' , "class" => "first", "link" =>"http://door-market/#1", "disable" =>""),
            array("title" => 'previous page', "class" => "previous", "link" =>"http://door-market/#1", "disable" =>""),
            array("title" => 1 , "class" => "page_num", "link" =>"http://door-market/#1", "disable" =>""),
            array("title" => 1 , "class" => "page_num", "link" =>"http://door-market/#2", "disable" =>""),
            array("title" => 1 , "class" => "page_num", "link" =>"http://door-market/#3", "disable" =>""),
            array("title" => 1 , "class" => "page_num", "link" =>"http://door-market/#4", "disable" =>"disabled"),
            array("title" => 1 , "class" => "page_num", "link" =>"http://door-market/#5", "disable" =>""),
            array("title" => 'next page' , "class" => "next", "link" =>"http://door-market/#6", "disable" =>""),
            array("title" => 'last page', "class" => "last", "link" =>"http://door-market/#77", "disable" =>"")
        );

        $category_list = array(
            "doors" => array("title" => 'Двері',"class" => "menu_category_item", "link" => "",
                "style" =>"",
                  "sub"   => array(
                           "all" => array("title" => 'Всі двері',"class" => "active_sub_category", "link" => "", "items_count" => 381),
                      "entrance" => array("title" => 'Вхідні',"class" => "sub_category", "link" => "", "items_count" => 381),
                         "rooms" => array("title" => 'Міжкімнатні',"class" => "sub_category", "link" => "", "items_count" => 381),
                         "glass" => array("title" => 'Під скло',"class" => "sub_category", "link" => "", "items_count" => 381),
                            "wc" => array("title" => 'Ванна-туалет',"class" => "sub_category", "link" => "", "items_count" => 381)
                                  )
                 ),
            "stairs" => array("title" => 'Сходи',"class" => "menu_category_item", "link" => "",
                "style" =>"",
                "sub"   => array(
                           "all" => array("title" => 'Всі сходи',"class" => "active_sub_category", "link" => "", "items_count" => 11),
                           "oak" => array("title" => 'Дубові сходи',"class" => "sub_category", "link" => "", "items_count" => 8),
                          "pine" => array("title" => 'Сходи з сосни',"class" => "sub_category", "link" => "", "items_count" => 3)
                )
            ),
            "beds" => array("title" => 'Ліжка',"class" => "menu_category_item", "link" => "",
                "style" =>"",
                "sub"   => array(
                           "all" => array("title" => 'Всі ліжка',"class" => "active_sub_category", "link" => "", "items_count" => 11),
                           "oak" => array("title" => 'Дубові ліжка',"class" => "sub_category", "link" => "", "items_count" => 8),
                          "pine" => array("title" => 'Соснові ліжка',"class" => "sub_category", "link" => "", "items_count" => 3)
                )
            ),
            "tables" => array("title" => 'Столи',"class" => "menu_category_item", "link" => "",
                "style" =>"",
                "sub"   => array(
                           "all" => array("title" => 'Всі столи',"class" => "active_sub_category", "link" => "", "items_count" => 11),
                           "oak" => array("title" => 'Столи з дуба',"class" => "sub_category", "link" => "", "items_count" => 8),
                          "pine" => array("title" => 'Столи з сосни',"class" => "sub_category", "link" => "", "items_count" => 3)
                )
            ),
            "fires" => array("title" => 'Псевдокаміни',"class" => "menu_category_item", "link" => "",
                "style" =>"",
                "sub"   => array(
                           "all" => array("title" => 'Всі псевдокаміни',"class" => "active_sub_category", "link" => "", "items_count" => 11)
                )
            ),
        );

        $path_list = array(
            array("title" => "Головна", "link" => ""),
            array("title" => "Двері", "link" => ""),
        );

        $total_items = $this->model->get_total_items_count($category,$sub_category,$this->page,$this->pagesize);

        $sort_list = $this->form_sort_list_url($category,$sub_category);

        $sort_list[$this->sort]['class'] = 'selected';

        $page_size_list = $this->form_pagesize_url($category,$sub_category);
/*
               echo '<pre>';
       var_dump($page_size_list);
       exit();*/



        $page_size_list[$this->pagesize]['class'] = 'selected';



        $this->set_links_for_menu($category_list);




        $pages[2]['value'] = $this->page;

        $pages[0]['link'] =$this->form_catalog_url($category,$sub_category, $first_page_mumber,$this->sort,$this->pagesize) ;
        $pages[1]['link'] =$this->form_catalog_url($category,$sub_category, $previous_page_number,$this->sort,$this->pagesize) ;

        $pages[7]['link'] =$this->form_catalog_url($category,$sub_category, $next_page_number,$this->sort,$this->pagesize) ;
        $pages[8]['link'] =$this->form_catalog_url($category,$sub_category, $last_page_number,$this->sort,$this->pagesize) ;

        $position = ($this->page%5 == 0)?5: $this->page%5;

        $this->log_to_console();

        for($i=2 ; $i<7;$i++){
            $page_num =  $this->page+ ($i-1) -$position;
            $pages[$i]['title'] =$page_num;
            $pages[$i]['link'] =$this->form_catalog_url($category,$sub_category, $page_num,$this->sort,$this->pagesize) ;

            if( ($i-1) ==$position){
                $pages[$i]['class'] = 'selected';
            }
            if(($i-1) > $last_page_number){
                $pages[$i]['disable'] = 'disabled';
            }
        }

        $category_list[$category]['sub']['all']['class'] = 'active_sub_category';
        $category_list[$category]['style'] = 'display: inline-block';

        $category_list[$category]['class'] = explode(" ",$category_list[$category]['class'])[0]." menu_active_category_item";

        $category_mame =  $this->model->get_category_name($category);

        $this->view->display_catalog($catalog_items,$pages,$category_list,$category_mame,$category,$sub_category,$total_items,$sort_list,$page_size_list);

        //echo "<pre>";
        //var_dump($catalog_items);

    }
    public function set_default_args(){
        $this->page = 1;
        $this->pagesize = 15;
        $this->sort = "cheap";
    }
    public function parse_args($args){

        //echo "<br>".$args;

        $parsed_args = explode(';',$args);

        if($args ==""){
            $this->set_default_args();
            return;
        }



        if(isset($parsed_args[0])){
            $page_arg = explode("=",$parsed_args[0]);
            //echo "<pre>";
            //var_dump($page_arg);
            if(isset($page_arg[0]) and isset($page_arg[1])){
                //echo (is_int((int)$page_arg[1]));
                if($page_arg[0] == "page" && is_int((int)$page_arg[1])){
                    $page = $page_arg[1];
                    //echo "page = ".$page;
                    $this->page = $page;
                }else{
                    echo "doesett set up";
                }
            }else{
                echo "doesett set up";
            }


        }
        if(isset($parsed_args[1])){
            $sort_arg = explode("=",$parsed_args[1]);
            //echo "<pre>";
            //var_dump($page_arg);
            if(isset($sort_arg[0]) and isset($sort_arg[1])){
                if($sort_arg[0] == "sort"){
                    $sort = $sort_arg[1];
                    //echo "sort = ".$sort;
                    $this->sort= $sort;
                }else{
                    echo "doesett set up 1";
                }
            }else{
                echo "doesett set up 2";
            }

        }
        if(isset($parsed_args[2])){
            $size_arg = explode("=",$parsed_args[2]);
            /*echo "<pre>";
            var_dump($size_arg);*/
            if(isset($size_arg[0]) and isset($size_arg[1])){
                if($size_arg[0] == "pagesize"){
                    $pagesize = $size_arg[1];
                    //echo "pagesize = ".$pagesize;
                    $this->pagesize = $pagesize;
                }else{
                    echo "doesett set up 1";
                }
            }else{
                echo "doesett set up 2";
            }
        }

    }
    public function form_catalog_url($category = null,$sub_category = null,$page = null, $sort = null, $page_size = null){
        $base_url = Host_config::$catalog_base_url;
        if(!isset($category)){
            return $base_url;
        }
        $result = $base_url.$category."/";
        $result = $result.$sub_category."/";


        $page_part = $this->form_url_args_element('page',$page);
        $result = $result.$page_part;

        $sort_part = $this->form_url_args_element('sort',$sort);
        $result = $result.$sort_part;

        $size_part = $this->form_url_args_element('pagesize',$page_size);
        $result = $result.$size_part;

        return $result;



    }

    public function form_url_args_element($arg_name=null,$value=null){
        if(!isset($arg_name)){
            return "";
        }
        if(!isset($value)){
            return "";
        }
        return $arg_name."=".$value.";";
    }
    public function form_pagesize_url($category = null,$sub_category = null){
        $tmp_res = array(
            "6" => array("class" => "", "title" =>"6", "link" => ""),
            "15" => array("class" => "", "title" =>"15", "link" => "")
        );

        foreach($tmp_res as &$res_item){




            $base_url = Host_config::$catalog_base_url;

        $result = $base_url.$category."/";
        $result = $result.$sub_category."/";


        $page_part = $this->form_url_args_element('page',$this->page);
        $result = $result.$page_part;

        $sort_part = $this->form_url_args_element('sort',$this->sort);
        $result = $result.$sort_part;

        $size_part = $this->form_url_args_element('pagesize',$res_item['title']);
        $result = $result.$size_part;

        $res_item['link'] = $result;
        }

        return $tmp_res;

    }

    public function form_sort_list_url($category = null,$sub_category = null){
        $sort_list = array(
            "default" =>array("class" => "", "title" =>"за замовчуванням", "link" => ""),
            "cheap" =>array("class" => "", "title" =>"за зростанням ціни", "link" => ""),
        );


        foreach($sort_list as $key => &$res_item){
            $base_url = Host_config::$catalog_base_url;

            $result = $base_url.$category."/";
            $result = $result.$sub_category."/";


            $page_part = $this->form_url_args_element('page',$this->page);
            $result = $result.$page_part;

            $sort_part = $this->form_url_args_element('sort',$key);
            $result = $result.$sort_part;

            $size_part = $this->form_url_args_element('pagesize',$this->pagesize);
            $result = $result.$size_part;

            $res_item['link'] = $result;
        }

        return $sort_list;
    }
    public function log_to_console(){
        //echo '<script type="text/javascript">';
        //echo 'console.log("Hello World!")';
        //echo '</script>';
    }

    public  function set_links_for_menu(&$category_list){
        $base_url = Host_config::$catalog_base_url;
        foreach($category_list as $key => &$category){
            $category['link'] = $base_url .$key."/all/";
            foreach($category['sub'] as $sub_key => &$sub_category){
                $sub_category['link'] = $base_url .$key."/".$sub_key;

                $sub_category['items_count'] = $this->model->get_items_count($key,$sub_key);

            }
        }

    }


} 